#include "communitypost.h"

namespace WordMemory {

    CommunityPost CommunityPost::fromJson(const QJsonObject &json)
    {
        CommunityPost post;
        post.id = json["id"].toInt();
        post.userId = json["user_id"].toInt();
        post.title = json["title"].toString();
        post.content = json["content"].toString();
        post.authorUsername = json["author_username"].toString("未知作者");
        post.createdAt = json["created_at"].toString();
        post.likesCount = json["likes_count"].toInt();
        post.sharesCount = json["shares_count"].toInt();
        post.isLikedByCurrentUser = json["is_liked_by_current_user"].toBool();
        return post;
    }

    QJsonObject CommunityPost::toJson() const
    {
        QJsonObject obj;
        obj["id"] = id;
        obj["user_id"] = userId;
        obj["title"] = title;
        obj["content"] = content;
        obj["author_username"] = authorUsername;
        obj["created_at"] = createdAt;
        obj["likes_count"] = likesCount;
        obj["shares_count"] = sharesCount;
        obj["is_liked_by_current_user"] = isLikedByCurrentUser;

        return obj;
    }
}
